package com.dayu.helper;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class EntryHelper {
    public static String DataBaseType;
    public static String DataBaseUrl;
    public static String DataBaseUser;
    public static String DataBasePwd;
    public static String DataBaseInit;
    public static String DataBaseSele;
    private static String jdbcUrl;
    public static String getJdbcUrl() {
        return jdbcUrl;
    }

    private static void setJdbcUrl(String newJdbcUrl) {
        String oldJdbcUrl = jdbcUrl;
        jdbcUrl = newJdbcUrl;
        support.firePropertyChange("jdbcUrl", oldJdbcUrl, newJdbcUrl);
    }
    public static String DriverClassName;

    public static final String HELPER_TEXT = "";
    private static final PropertyChangeSupport support = new PropertyChangeSupport(EntryHelper.class);

    public static List<String> enableDataBaseType = Arrays.asList(new String[]{"MySQL", "Oracle", "PostgreSQL","SqlServer"});


    public static List<String> QueryDataBaseList() throws  Exception{
        switch (DataBaseType){
            case "MySQL":
                return MySQLHelper.QueryDataBaseList();
            case "Oracle":
                return OracleHelper.QueryDataBaseList();
            case "PostgreSQL":
                return PostgreSQLHelper.QueryDataBaseList();
            case "SqlServer":
                return SQLServerHelper.QueryDataBaseList();
            default:
                throw new Exception("数据库未适配");
        }

    }

    public static List<String> QueryAllTable(String dbBase) throws  Exception{
        DataBaseSele = dbBase;
        List<String> result = new ArrayList<>();
        switch (DataBaseType){
            case "MySQL":
                result = MySQLHelper.QueryAllTable();
                setJdbcUrl(MySQLHelper.DataBaseContentUrl);
                break;
            case "Oracle":
                result = OracleHelper.QueryAllTable();
                setJdbcUrl(OracleHelper.DataBaseContentUrl) ;
                break;
            case "PostgreSQL":
                result = PostgreSQLHelper.QueryAllTable();
                setJdbcUrl(PostgreSQLHelper.DataBaseContentUrl);
                break;
            case "SqlServer":
                result = SQLServerHelper.QueryAllTable();
                setJdbcUrl(SQLServerHelper.DataBaseContentUrl);
                break;
            default:
                throw new Exception("数据库未适配");
        }
        return result;

    }

    public static boolean verifyConnection(String dbType, String dbUrl, String dbUser, String dbPwd, String dbBase) throws Exception {
        boolean isContent = false;
        String dbDriver= "";
        DataBaseType = dbType;
        DataBaseUrl = dbUrl;
        DataBaseUser = dbUser;
        DataBasePwd = dbPwd;
        DataBaseInit = dbBase;

        switch (dbType){
            case "MySQL":
                dbDriver = MySQLHelper.DriverClassName;
                isContent = MySQLHelper.verifyConnection();
                setJdbcUrl(MySQLHelper.DataBaseContentUrl);
                break;
            case "Oracle":
                dbDriver = OracleHelper.DriverClassName;
                isContent = OracleHelper.verifyConnection();
                setJdbcUrl(OracleHelper.DataBaseContentUrl);
                break;
            case "PostgreSQL":
                dbDriver = PostgreSQLHelper.DriverClassName;
                isContent = PostgreSQLHelper.verifyConnection();
                setJdbcUrl(PostgreSQLHelper.DataBaseContentUrl);
                break;
            case "SqlServer":
                isContent = SQLServerHelper.verifyConnection();
                setJdbcUrl(SQLServerHelper.DataBaseContentUrl);
                dbDriver = SQLServerHelper.DriverClassName;
                break;
            default:
                isContent = false;
                throw new Exception("数据库未适配");
        }
        if(isContent){
            DataBaseType = dbType;
            DataBaseUrl = dbUrl;
            DataBaseUser = dbUser;
            DataBasePwd = dbPwd;
            DataBaseInit = dbBase;
            DriverClassName = dbDriver;
            return isContent;
        }
        return isContent;
    }
    public static void addPropertyChangeListener(PropertyChangeListener pcl) {
        support.addPropertyChangeListener(pcl);
    }

    public static void removePropertyChangeListener(PropertyChangeListener pcl) {
        support.removePropertyChangeListener(pcl);
    }
}
